 <?php
  use yii\helpers\Html;
  use yii\helpers\Url;
  use yii\bootstrap\ActiveForm;
  use yii\widgets\LinkPager;

  $this->title = 'authlist';
 ?>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="dist/css/AdminLTE.min.css">
<style>
	.limit_choose .limit_uname label{font-size:16px;}
	.limit_choose .limit_uname span{margin-left:30px;}
	.limit_choose .check_limit>label{font-size:16px;}
	.limit_choose .limit_role label{font-size:16px;}
	.limit_choose .check_limit .checkbox{width:50%;float:left;margin-top:10px;font-size:15px;}
	.limit_choose .box-footer{text-align:center;}
	.limit_choose .box-footer button{width:200px;}
	.font-info{margin-left:450px;}
</style>
<section class="content">
	<div class="box box-primary">
		<div class="box-header with-border">
			<h3 class="box-title">角色管理</h3>
		</div>
		<strong style="color:green;" class="font-info">
		    <?php if(Yii::$app->session->hasFlash('success')){
		        echo Yii::$app->session->getFlash('success');
		    }?>
	    </strong>
	    <button type="button" class="btn btn-primary add_role" style="float:right">新建角色</button>
		<div class="box-body limit_choose">
			<?=Html::beginForm(Url::toRoute(['user/updaterole']),'post',['class'=>'form']);?>
				<div class="box-body">
					<div class="form-group limit_role">
						<label>选择角色</label>
						<input type="hidden" name="_csrf" value="<?=Yii::$app->request->getCsrfToken() ?>">
						<select class="form-control" name="rolename">
							<option>请选择角色</option>
							<?php foreach($roles as $val) :?>
								<option value="<?= $val->name?>"><?= $val->description?></option>
							<?php endforeach;?>	
						</select>
					</div>
					<div class="form-group check_limit">
						<label>权限分配</label>
						<div style="overflow:hidden;" id="authbox">
							<?php foreach ($auth as $val): ?>
				           		<div class="checkbox"><label><input name="auth[]" type="checkbox"  value="<?= $val->name?>" /><?= $val->description?></label></div>
				           	<?php endforeach;?>
			            </div>
					</div>
				</div>
				<div class="box-footer">
					<button type="submit" class="btn btn-primary">提交</button>
				</div>
			<?=Html::endForm();?>
		</div>
	</div>
</section>
<script type="text/javascript" src="js/jquery.js"></script>
<script>
	Array.prototype.contains = function (obj) {
	  var i = this.length;
	  while (i--) {
	    if (this[i] === obj) {
	      return true;
	    }
	  }
	  return false;
	}

	$(".box-primary").on('change', '.limit_role select', function(){
		var role=$(this).val();
		var url = "<?= Url::toRoute('user/getauth')?>";
		var _csrf ="<?= Yii::$app->request->getCsrfToken()?>";
		$.ajax({
			url:url,
			type:'GET',
			dataType:'json',
			data: {
				role:role,
				_csrf:_csrf
			},
			success: function(result){
			   var html = "";

	           $.each(result.roles, function (n, value) {
	           		if (result.checked.contains(n)) {
						html += "<div class='checkbox'><label><input checked type='checkbox' name='auth[]' value='"+n+"'>"+value+"</label></div>";
	           		} else {
	           			html += "<div class='checkbox'><label><input type='checkbox' name='auth[]' value='"+n+"'>"+value+"</label></div>";
	           		}
	            });

				$("#authbox").html(html);
			},
			error:function(){
			  alert('网络忙');
			}
		});
	});

	/*新建角色*/
	$(".box-primary").on('click', '.add_role', function(){
		var role_html = "<label>新建角色</label>\n<input type='text' name='rolename' class='form-control' placeholder='请输入角色名' />";
		$(this).removeClass("add_role").addClass("update_role");
		$(this).text("修改角色");
		$(".limit_role").html(role_html);
	});

	/*修改角色*/
	$(".box-primary").on('click', '.update_role', function(){
		var role_html = "<label>选择角色</label>\n<select class='form-control' name='rolename'>\n<option>请选择角色</option>\n<?php foreach($roles as $val) :?>\n<option value='<?= $val->name?>'><?= $val->description?></option>\n<?php endforeach;?></select>";
		$(this).removeClass("update_role").addClass("add_role");
		$(this).text("新建角色");
		$(".limit_role").html(role_html);
	});
</script>

 